package com.ssyc.gsk_teacher_appraisal.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.Constants;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.BqaManager;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_teacher_appraisal.R;
import com.ssyc.gsk_teacher_appraisal.adapter.TableRvAdapter;
import com.ssyc.gsk_teacher_appraisal.bean.TableInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * 学生分析界面
 * Created by Administrator on 2018/10/9 0009.
 */

public class TkTeacherAppStudentAnalyzeActivity extends BaseActivity implements View.OnClickListener {
    private ImageView ivBack;
    private RecyclerView rv;
    private LinearLayout llContent;
    private RelativeLayout rlLoading;

    //数据集合
    private List<TableInfo.ListBean> oldDatas;
    //适配器
    private TableRvAdapter adapter;

    //请求的参数
    private String tel, exam_id;

    /**
     * 跳转到当前页面的方法
     *
     * @param context
     * @param ttel
     * @param exam_id
     */
    public static void startActivity(Context context, String ttel, String exam_id) {
        Intent intent = new Intent(context, TkTeacherAppStudentAnalyzeActivity.class);
        intent.putExtra("ttel", ttel);
        intent.putExtra("exam_id", exam_id);
        context.startActivity(intent);
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_teacher_app_student_analyze;
    }

    @Override
    protected void init() {

        showContent();

        initIntent();

        //初始化视图
        initView();

        //初始化列表
        initRv();

        //请求服务器数据
        http(BqaManager.NORML);
    }


    /**
     * 获取传递的参数
     */
    private void initIntent() {
        tel = getIntent().getStringExtra("ttel");
        exam_id = getIntent().getStringExtra("exam_id");
    }


    /**
     * 初始化视图
     */
    public void initView() {
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        rv = findViewById(R.id.rv);
        llContent = findViewById(R.id.ll_content);
        rlLoading = findViewById(R.id.rl_loading);
        llContent.setVisibility(View.VISIBLE);
        rlLoading.setVisibility(View.GONE);
    }

    /**
     * 请求服务器数据
     */
    private void http(int method) {
        Map<String, String> map = new HashMap<>();
        map.put("type", "4");
        map.put("ttel", tel);
        map.put("exam_id", exam_id);
        HttpUtils.post(HttpAdress.STUDENTANALYZE, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                BqaManager.setErrorNet(rlLoading, null, method, adapter);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if (llContent != null && llContent.getVisibility() != View.VISIBLE) {
                    llContent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    TableInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, TableInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            List<TableInfo.ListBean> newDatas = info.getList();
                            if (newDatas != null) {
                                if (method == BqaManager.NORML) {
                                    oldDatas.clear();
                                    oldDatas.addAll(newDatas);
                                    BqaManager.setRv(null, TkTeacherAppStudentAnalyzeActivity.this, adapter, rv);
                                }
                            }
                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }


    /**
     * 初始化表格列表
     */
    private void initRv() {
        oldDatas = new ArrayList<>();
        adapter = new TableRvAdapter(this,this, R.layout.teacher_app_rv_tiem_student_analyze, oldDatas);
    }

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }
}
